package proyecto.BusinessLogic;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import proyecto.DataAccessObject.PersonaDAORemote;
import proyecto.EntityBeans.Persona;

@Stateless
@Remote(PersonaBLRemote.class)
public class PersonaBL implements PersonaBLRemote{

	PersonaDAORemote personaDAORemote;
	
	public int save(Persona persona) {	
		
		try
		{
			Context context = new InitialContext();
			personaDAORemote = (PersonaDAORemote) context.lookup("PersonaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return personaDAORemote.save(persona); //alta de persona		
		
	}
		
	public Persona findById(int id) {
		try
		{
			Context context = new InitialContext();
			personaDAORemote = (PersonaDAORemote) context.lookup("PersonaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
			
		return personaDAORemote.findById(id);
		
	}
	
		
	public void merge(Persona persona) {
		try
		{
			Context context = new InitialContext();
			personaDAORemote = (PersonaDAORemote) context.lookup("PersonaDAO/remote");
			personaDAORemote.merge(persona);
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}	
		
	}
	
	public List findBySurname(String surname)
	{	
		try
		{
			Context context = new InitialContext();
			personaDAORemote = (PersonaDAORemote) context.lookup("PersonaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return personaDAORemote.findBySurname(surname);
	}
	
	public List findAll() {		
		try
		{
			Context context = new InitialContext();
			personaDAORemote = (PersonaDAORemote) context.lookup("PersonaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return personaDAORemote.findAll();
	}
	
	public List findByIdList(int id)
	{	     
		try
		{
			Context context = new InitialContext();
			personaDAORemote = (PersonaDAORemote) context.lookup("PersonaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	    return personaDAORemote.findByIdList(id);
	}
	
		
	public void remove(int idPersona){	
		try
		{
			Context context = new InitialContext();
			personaDAORemote = (PersonaDAORemote) context.lookup("PersonaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		personaDAORemote.remove(idPersona);
	}
	
	public void refresh(Persona persona){
		try
		{
			Context context = new InitialContext();
			personaDAORemote = (PersonaDAORemote) context.lookup("PersonaDAO/remote");
		} catch (NamingException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		personaDAORemote.refresh(persona);
	}
}
